// Code Behind for createcondialog.mxml
      	import mx.controls.Text;
       	import mx.messaging.channels.StreamingAMFChannel;
        import mx.managers.PopUpManager;
		import mx.controls.Alert;
		import flash.net.FileReferenceList;
		import flash.net.FileFilter;
		import flash.net.URLLoader;
		import flash.net.URLRequest;
		import flash.events.Event;
		import jcrweb.as3.FlexUtil;
		import jcrweb.as3.RequestUtil;
		import flexlib.containers.VAccordion;
		import jcrweb.as3.LanguageUtil;
		
		private var pendingFiles:Array;
		private var uploadedFileName:Array;
		private var fileList:FileReferenceList;
		private var uploadUrl:URLRequest;
		
		private var uploadingFlag:int = 0;
		
		[Bindable]
		private var strPath:String = "";
		[Bindable]
		private var strPathName:String = "";
		[Bindable]
		private var strId:String = "";
		[Bindable]
		private var createrCode:String = "";
		[Bindable]
		private var createrName:String = "";
		[Bindable]
		private var languageUtil:LanguageUtil;
		[Bindable]
		[Embed(source='../WebContent/images/attent.gif')]
		private var iconAlert:Class;
		[Bindable]
		[Embed(source='../WebContent/images/error.gif')]
		private var iconError:Class;
		
		public function setCreaterCode(createrCode:String):void{
			this.createrCode = createrCode
		}		
		public function setCreaterName(createrName:String):void{
			this.createrName = createrName
		}
		public function setId(strId:String):void{
			this.strId = strId;
		}
		public function setPath(path:String):void{
			this.strPath = path;
		}
		public function setPathName(pathName:String):void{
			this.strPathName = pathName;
		}
		
		private function init():void{
			this.fileList = new FileReferenceList();		
			this.fileList.addEventListener(Event.SELECT,selectHandler);		
			var header:URLRequestHeader = new URLRequestHeader("Content-Type", "multipart/form-data");
			this.uploadUrl = new URLRequest(RequestUtil.INITCONTENT_URL+"?path="+this.strPath);
			this.uploadUrl.method = URLRequestMethod.POST;
			this.uploadUrl.requestHeaders.push(header);
			
			languageUtil = LanguageUtil.getInstance();
		}
		private function selectHandler(event:Event):void{
			var len:int = event.currentTarget.fileList.length;
			if(len > 10){
				len = 10;
			}
			this.pendingFiles = new Array();
			this.uploadedFileName = new Array();
			
			for(var j:int=0;j<10;j++){
				this["txt"+(j+1)].text = "";
			}
			for(var i:int=0;i<len;i++){
				this.addPengingFiles(event.currentTarget.fileList[i]);
				this["txt"+(i+1)].text=event.currentTarget.fileList[i].name;
			}
		}
		private function addPengingFiles(file:FileReference):void{
			this.pendingFiles.push(file);
		}
		//===============file upload ======================
		private function selected():void{
			this.fileList.browse();
		}
		private function getTypes():Array{
			//var allTypes:Array = new Array();
			return null;
		}
		private function complete(event:Event):void {
			var loader:URLLoader = URLLoader(event.target); 
			var isError:String = FlexUtil.getTag(loader.data,FlexUtil.IS_ERROR);
			if(isError == "true" ){
				var errorMes:String = FlexUtil.getTag(loader.data,FlexUtil.ERROR_MES_TAG);
				Alert.show("Create directory faild!"+errorMes,"Error",0,null,null,iconError);
			}
			closeDialog();
		}
		private function closeDialog():void{
			PopUpManager.removePopUp(this);
		}
		private function upload():void{
			for(var i:int;i<this.pendingFiles.length;i++){
				var file:FileReference = FileReference(this.pendingFiles[i]);
				file.addEventListener(Event.COMPLETE,completeHandler);
				file.addEventListener(ProgressEvent.PROGRESS,progressHandler);
				file.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
				file.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
				file.upload(uploadUrl);
			}
		}
		private function completeHandler(event:Event):void{
			var file:FileReference = FileReference(event.target);
			this.uploadedFileName.push(file.name);
			this.removePendingFiles(file.name);
		}
		private function removePendingFiles(filename:String):void{
			for(var i:int;i<this.pendingFiles.length;i++){
				if(this.pendingFiles[i].name == filename){
					this.pendingFiles.splice(i,1);
					break;
				}
			}
			if(this.pendingFiles.length == 0){
				doOnComplete();
			}
		}
		private function doOnComplete():void{
			uploadAlert.visible = false;
			this.uploadingFlag = 2;
			var para:String = new String();
			para +="&path="+input_path.text;
			para +="&name="+input_name.text;
			para +="&createrCode="+input_createrCode.text;
			para +="&createrName="+input_createrName.text;
			para +="&note="+input_note.text;
			if(this.uploadedFileName!=null){
				for(var i:int = 0;i<this.uploadedFileName.length;i++){
					var fileName:String = this.uploadedFileName[i];
					para +="&fileName"+(i+1)+"="+fileName;
				}
			}
			para =  encodeURI(para);
			para =  encodeURI(para);
			var url:String = RequestUtil.CREATECONTENT_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,complete);
		}
		private function progressHandler(event:ProgressEvent):void{
			var file:FileReference = FileReference(event.target);
			for(var i:int=0;i<10;i++){
				if(this["txt"+(i+1)].text == file.name){
					var fileUploadPercent:uint = (event.bytesLoaded / event.bytesTotal) * 100;
					this["pgb"+(i+1)].setProgress(fileUploadPercent,100);
				}
			}
//			var fileUploadPercent:uint = (event.bytesLoaded / event.bytesTotal) * 100;
//			lab.text = "Complete:"+event.bytesLoaded+" bytes" + "  Total:"+event.bytesTotal;
//			progress.setProgress(fileUploadPercent,100);
		}
		private function ioErrorHandler(event:IOErrorEvent):void{
			var file:FileReference = FileReference(event.target);
			Alert.show("IOErrorException:"+file.name+" event:"+event.toString(),"Error",0,null,null,iconError);
			file.cancel();
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void{
			var file:FileReference = FileReference(event.target);
			Alert.show("SecurityErrorException:"+file.name+" event:"+event.toString(),"Error",0,null,null,iconError);
			file.cancel();
		}	
		private function removeFiles(filename:String):void{
            if(filename == ""){
                return;
            }
			if(this.uploadingFlag == 0){
				for(var i:int;i<this.pendingFiles.length;i++){
					if(this.pendingFiles[i].name == filename){
						this.pendingFiles.splice(i,1);
						break;
					}
				}
			}
			if(this.uploadingFlag == 1){
				for(var j:int;j<this.pendingFiles.length;j++){
					var file:FileReference = FileReference(this.pendingFiles[j]);
					if(file.name == filename){
						file.cancel();
						this.pendingFiles.splice(j,1);
						break;
					}
				}
				for(var z:int=0;z<10;z++){
					if(this["txt"+(z+1)].text == filename){
						this["pgb"+(z+1)].setProgress(0,100);
					}
				}
				if(this.pendingFiles.length == 0){
					doOnComplete();
				}				
			}
		}
		//===============file upload ======================
		private function submit():void{
			if(this.uploadingFlag == 1){
				Alert.show("The files uploading now! Cannot submit.","Attention",0,null,null,iconAlert);
				return;
			}
			if(input_name.text == ""){
				Alert.show("please input name.","Error",0,null,null,iconError);
				return;
			}
			if(this.pendingFiles!=null && this.pendingFiles.length > 0){
				this.uploadingFlag = 1;
				this.input_createrCode.editable = false;
				this.input_createrName.editable = false;
				this.input_name.editable = false;
				this.input_note.editable = false;
				//uploading
				upload();
				uploadAlert.visible = true;
			}else{
				doOnComplete();
			}
		}
		private function close():void{
			this.cancel();
			this.closeDialog();
		}
		private function cancel():void{
			uploadAlert.visible = false;
			this.uploadingFlag = 0;
			if(this.pendingFiles!=null){
				for(var i:int;i<this.pendingFiles.length;i++){
					var file:FileReference = FileReference(this.pendingFiles[i]);
					file.cancel();
				}
			}
			this.pendingFiles = null;
			for(var j:int=0;j<10;j++){
				this["txt"+(j+1)].text = "";
				this["pgb"+(j+1)].setProgress(0,100);
			}
			this.input_createrCode.editable = true;
			this.input_createrName.editable = true;
			this.input_name.editable = true;
			this.input_note.editable = true;
			this.input_name.text = "";
			this.input_note.text = "";
		}
		private function keyDownEvent(keyCode:uint):void{
				if(keyCode == 27){
					this.closeDialog();
				}
		}